/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-13 10:32:35
 * @LastEditors: TT
 * @LastEditTime: 2022-07-17 18:15:56
 */

import 'package:flutter/material.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:hzy_template/config/dataconfig/language_info_config.dart';
import '../../../components/common_index.dart';
import '../../../config/config_index.dart';
import '../../../config/controller/user_state_controller.dart';
import '../../../config/models/tf_config_model.dart';
import '../../../config/models/user_model.dart';
import '../models/normal_data.dart';

class LoginC extends CommonGetXController {
  RxList<TfConfigModel> get data => NormalData().loginInfo.obs;

  /// 是否显示 密码
  Rx isvisibility = false.obs;

  /// 错误提示语
  Rx errormsg = "".obs;

  /// 是否同意
  RxBool agreement = false.obs;

  /// 配显示隐藏 密码
  void configpasswordtap() {
    isvisibility.value = !isvisibility.value;
  }

  /// 点击登录
  void taplogin() async {
    String msg = '';
    for (var item in data) {
      if (item.text == null) {
        msg = item.hintText ?? "";
        break;
      }
    }

    if (judgeisagreement()) {
      if (msg != "") {
        showtoastmsg(msg);
      } else if (agreement.value) {
        UserModel model = UserModel();
        model.nickname = data[0].text;
        model.pwd = data[1].text;
        configlogin(model: model);
      }
    }
  }

  bool judgeisagreement() {
    if (!agreement.value) {
      showtoastmsg(
        configLanguageMessage(
          LanguageConfig.agreeagreement,
        ),
      );
    }
    return agreement.value;
  }

  void changepassword(String value) {
    data[1].text = value;
    TfConfigModel model = data[1];
    data[1] = model;
  }

  /// 点击 急速登录
  void tapquicklogin() {
    showPermissionpopup(
      context: Get.context!,
      dialogBackColor: HzyColorConfig().whitebackgroundColor,
      suremsg: configLanguageMessage(LanguageConfig.agree),
      cannermsg: configLanguageMessage(LanguageConfig.disagree),
      msgwidget: Text(
        configLanguageMessage(LanguageConfig.oneloginpopmsg),
        style: FontConfig().fontBold16Black,
      ),
      ontapsure: () async {
        UserModel model = UserModel();
        model.nickname = LauchNormalMsg.hometitle;
        model.pwd = "111111";
        configlogin(model: model);
      },
    );
  }

  void configlogin({required UserModel model}) async {
    await UserStateController.current.loadSuccess(model);
    showtoastmsg(configLanguageMessage(LanguageConfig.loginsuccess), ontap: () {
      currentTo(name: PageIdConfig.index);
    });
  }

  /// 点击 用户协议
  void tapagreement() {
    agreement.value = !agreement.value;
  }
}
